var commoneRequestUrl = 'https://zhzx.on-line-demo.com/api' //接口统一访问链接
//接口封装
export default function upload(params) {
	let showLoading = false
    // Promise 异步编程的一种解决方案
    var promise = new Promise((resolve, reject) => {
		var timeout = setTimeout(() => {
			if(timeout) clearTimeout(timeout)
			if(!showLoading) return false
			showLoading = true
			uni.showLoading({
				title: '上传中'
			});
		}, 1000)
        uni.uploadFile({
        	url: commoneRequestUrl + params.url, 
			header: {
                'token': uni.getStorageSync('userInfo').token,//传token(看后端需不需要)
            },
        	filePath: params.file,
        	name: 'file',
        	// formData: {
        	// 	uid: uni.getStorageSync('uid') || ''
        	// },
            success: function(res) {
				if(res.data.code == '500'){
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000
					});
				}else{
					resolve(res.data);
				}
				if(showLoading){
					showLoading = false
					uni.hideLoading();
				}
            },
            fail: function(err) {
                // console.log(err); //接口访问失败
                reject(err);
				if(showLoading){
					showLoading = false
					uni.hideLoading();
				}
            }
        })
    });
    return promise;
}
